'''Clue-less game driver file'''

import os.path as path
import os
import sys

if __name__ == '__main__':
    src = path.normpath(os.getcwd())
    shared = path.normpath(os.path.join(src, 'shared'))
    frontend = path.normpath(os.path.join(src, 'frontend'))
    backend  = path.normpath(os.path.join(src, 'backend'))

    for p in [src, shared, frontend, backend]:
        if p not in sys.path:
            sys.path.append(p)



import Queue
import socket
import pickle

import time
import game_constants
import game_thread
import server
import client
import proxy_receiver
import proxy_sender




def launch_thread(factory, *args):
    t = factory(*args)
    tq = t.in_q
    t.start()
    return (t, tq)

def join_thread((t, tq)):
    t.join

def ntfy(dest, *args):
    game_thread.GameThread.ntfy(dest[1], *args)


g_shared_game_id = 'game 0'

cl = None
s  = None
p = None
g_character = ''

def connect_to_server(my_name, addr, port = None):
    global cl
    global s
    global p
    
    port = int(port) if port is not None else 50007
    s = socket.create_connection((addr, port))
    cl = launch_thread(client.CluelessClient, my_name)
    p = proxy_receiver.ProxyReceiver( 'client %s receiver' % my_name, cl[1], s)
    p.start()

    ntfy(cl, 'connect_to_server', proxy_sender.QueueWrapper(s))

# def list_games():
#     ntfy(cl, 'list_games')

def new_game(game_id):
    ntfy(cl, 'new_game', game_id)

def join_game(game_id, character):
    global g_character
    g_character = character
    ntfy(cl, 'join_game', game_id, character)

def start_game():
    ntfy(cl, 'start_game')

def move_player(new_position):
    ntfy(cl, 'move_player', g_character, new_position)
    
def make_suggestion(name, weapon, room):
    ntfy(cl, 'make_suggestion', name, weapon, room)

def suggestion_response(to_client, response):
    ntfy(cl, 'suggestion_response', to_client, response)

def end_turn():
    ntfy(cl, 'turn_completed')


def make_accusation(player, weapon, room):
    ntfy(cl, 'make_accusation', player, weapon, room)

def query_cards():
    ntfy(cl, 'query_cards')

def query_house():
    ntfy(cl, 'query_house')

def main(n, host_address, host_port = None, *args):
    pass
    # game_thread.GameThread.ntfy(clq, 'connect_to_server', proxy_sender.QueueWrapper(s))

    # time.sleep(1)

    # if '0' in n:
    #     print 'generating new game'
    #     game_thread.GameThread.ntfy(clq, 'new_game', game_id)

    # time.sleep(4)

    # game_thread.GameThread.ntfy(clq, 'join_game', game_id, character)

    # time.sleep(5)

    # if n == '2':
    #     game_thread.GameThread.ntfy(clq, 'start_game')

    # if n == '0':
    #     game_thread.GameThread.ntfy(clq, 'move_player', 'Miss Scarlet', (1,4))
    #     time.sleep(1)
    #     game_thread.GameThread.ntfy(clq, 'make_suggestion', 'Miss Scarlet', 'Wrench', 'Study')

    # time.sleep(5)
    # if n == '1':
    #     game_thread.GameThread.ntfy(clq, 'suggestion_response', 'client 0', None)

    # time.sleep(5)
    # if n == '2':
    #     game_thread.GameThread.ntfy(clq, 'suggestion_response', 'client 0', None)

    # time.sleep(5)
    # if n == '0':
    #     game_thread.GameThread.ntfy(clq, 'turn_completed')

    # time.sleep(5)
    # if n == '2':
    #     game_thread.GameThread.ntfy(clq, 'turn_completed')

    # time.sleep(5)
    # if n == '1':
    #     game_thread.GameThread.ntfy(clq, 'make_accusation', 'Mr. Green', 'Wrench', 'Hall')
    

    # cl.join()
    # p.join()


if __name__ == '__main__':
    if len(sys.argv) <= 3:
        print 'Usage: python clue-less-client.py id character host-address [host-port]'
    else:
        main(*sys.argv[1:])




